Skip to content

Qwen 3.5 MoE: Add --backend metal export path#18880

Open
manuelcandales wants to merge 4 commits intogh/manuelcandales/174/headfrom
gh/manuelcandales/175/head
Open

Qwen 3.5 MoE: Add --backend metal export path#18880
manuelcandales wants to merge 4 commits intogh/manuelcandales/174/headfrom
gh/manuelcandales/175/head

Conversation

@manuelcandales
Copy link
Copy Markdown
Contributor

Adds Metal backend support to export.py via --backend metal flag:

  • _prepare_and_quantize_metal: applies source transforms, quantizes
    experts to MLX affine INT4, quantizes non-expert layers with fpa4w
    (skips shared_expert_gate with N<4 for prefill compatibility)
  • _export_metal: exports decode + prefill methods via MetalBackend/
    MetalPartitioner

CUDA and MLX paths are unchanged.

Authored with Claude.

[ghstack-poisoned]
@manuelcandales manuelcandales requested a review from lucylq as a code owner April 14, 2026 16:25
@pytorch-bot
Copy link
Copy Markdown

pytorch-bot bot commented Apr 14, 2026

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/18880

Note: Links to docs will display an error until the docs builds have been completed.

❌ 2 Cancelled Jobs

As of commit f8ff857 with merge base 5707e2a (image):

CANCELLED JOBS - The following jobs were cancelled. Please retry:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

[ghstack-poisoned]
[ghstack-poisoned]
@manuelcandales manuelcandales requested review from mergennachin and metascroy and removed request for lucylq April 15, 2026 15:14
Comment thread examples/models/qwen3_5_moe/export.py Outdated
quantize_experts_metal(model, config, args.qlinear_group_size)

# Untie lm_head/embedding for independent quantization
if model.lm_head.weight.data_ptr() == model.embed_tokens.weight.data_ptr():
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the model have tied embeddings?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that tie_word_embeddings defaults to False. I verified that I didn't need this, and just cleaned it up.

Comment thread examples/models/qwen3_5_moe/export.py Outdated
if args.qlinear:
import torchao.experimental.ops.mps # noqa: F401
from torchao.experimental.quant_api import UIntxWeightOnlyConfig
from torchao.quantization.quant_api import quantize_
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can this path not use quantize_model_?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Simplified the code here using quantize_model_. This required updating quantize.py with the compatible shape check for the Metal backend quantization

[ghstack-poisoned]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants